package utils

import (
	"fmt"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"runtime/debug"
	"strings"
)

func SetUp() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				DebugStack := ""
				for _, v := range strings.Split(string(debug.Stack()), "\n") {
					DebugStack += v + "\n"
				}
				fmt.Printf(DebugStack)
				log.Error(DebugStack)
				c.JSON(500, err)
			}
		}()
		c.Next()
	}
}
